<?php

namespace Home\Controller;

use Think\Controller;
use Think\Page;
use Think\Upload;

class backstageController extends Controller
{

    public function backstage_add()
    {
        $data = M('ry_product');
        $f = $data->create();
        $p = $_POST['product_num'];
        if ($f) {
            $data->add($f);
            $id = $data->getFieldByProduct_num($p, 'id');
            $sql = M('ry_img');
            $img = $_FILES['img'];
            $path = $this->upload($img);
            $arr['product_id'] = $id;
            $arr['image'] = $path;
            $sql->add($arr);
            $this->success('操作成功', U('shop_list'), 3);
        } else {
            $this->error('输入错误');
        }
    }

    public function upload($file)
    {
        $upload = new Upload();
        $upload->mimes = array('image/png', 'image/jpeg', 'image/gif');

        $info = $upload->uploadOne($file);
        if ($info) {
            $path = $info['savepath'] . $info['savename'];  //路径名（自动新建目录）  加上文件名
            return $path;
        } else {
            echo $upload->getError();
            return '';
        }
    }

    public function shop_list()
    {
        header('content-type:text/html;charset=utf-8');
        $p = I('get.p');
        $search = i('post.search');
        $data = M('ry_product');
        $this->pager = new Page($data->count(), 8);
        $all['product_name'] = array("like", "%$search%");
        $shop = $data->order('id desc')->where($all)->page($p, 8)->select();
        $this->assign('list', $shop);
        $this->assign('pager', $this->pager);
        $this->display();
    }

    public function del()
    {
        $id = $_GET['id'];
        $data = M('ry_product');
        $data->startTrans();
        $data->delete($id);
        $ff = $data->table("ry_img")->where("product_id=$id")->delete();
        $data->commit();
        echo $data->getLastSql();
        if ($ff) {
            $this->success('操作成功', '', 3);
        } else {
            $this->error('数据错误');
        }
    }

    public function edit_shop()
    {
        $data = M('ry_product');
        $id = $_GET['id'];
        $ff = $data->where("ry_product.id=$id")->join('inner join ry_img on ry_img.product_id=ry_product.id')->select();
        $ff = $ff[0];
        var_dump($ff);
        if ($ff) {
            $this->assign('list', $ff);
            $this->display();
        } else {
            $this->error('数据没找到');
        }
    }
    public function edit_shopsave()
    {
        header('content-type:text/html;charset=utf-8');
        $data = M('ry_product');
        $f = $data->create();
        $idd = $_POST['id'];
        var_dump($f);
        $p = $_POST['product_num'];
        if ($f) {
            $data->where("id=$idd")->save($f);
            $id = $data->getFieldByProduct_num($p, 'id');
            $sql = M('ry_img');
            $img = $_FILES['img'];
            $path = $this->upload($img);
            $arr['product_id'] = $id;
            $arr['image'] = $path;
            $sql->save($arr);

        } else {

            $this->error('输入错误');
        }
    }

//添加管理员 开始
    public function Add_admin()
    {
        $U = M('RyUser');
        $user = $U->create();
        if ($user) {
            $c['username'] = $user['username'];
            if ($U->where($c)->count()) {
                $this->error('用户名已存在');
            } else {
                $user['password'] = md5($user['password']);
                $U->add($user);
                $this->success('添加成功', U('add_admit'));
            }
        } else {
            $this->error('请填写数据');
        }
    }



//添加管理员 结束

//    判断是否管理员登录 开始

    protected function _initialize()
    {

        C('LAYOUT_NAME', 'houtai');
        C('LAYOUT_ON', true);

        $logon_rights = ['Add_admin', 'edit_shopsave', 'edit_shop', 'del', 'shop_list', 'backstage_add', 'upload', 'backstage'];
        if (in_array(ACTION_NAME, $logon_rights)) {
            if (session('user_id')) {
                $U = M('RyUser');
                $data = $U->find($_SESSION['user_id']);
                if ($data) {
                    if ($data['gm'] < 2) {
                        $this->assign('users', $data);
                    } else {
                        $this->error('权限不足');
                    }
                } else {
                    $this->error('用户不存在');
                }
            } else {
                $this->error('您还没登录呢,请先登录');
            }
        }

    }

    public function backstage(){

        $this->display();
    }

}





